package Uitls;

import java.text.DecimalFormat;

/**
 * Created by Administrator on 2016/7/18.
 */
public class TrafficUtils {
    public static String getConversionWithUnitNameForMonthly(long size)
    {
        if (size <= 0)
            return "0 B";
        final String[] units = new String[] { "B", "KB", "MB", "GB", "TB" };
        int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
        if (digitGroups > 4)
        {
            return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, 4)) + " " + units[4];
        }
        return new DecimalFormat("###0").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
    }

    public static String getConversionWithUnitName(long size)
    {
        if (size <= 0)
            return "0 B";
        final String[] units = new String[] { "B", "KB", "MB", "GB", "TB" };
        int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
        if (digitGroups > 4)
        {
            return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, 4)) + " " + units[4];
        }
        return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
    }

}
